package com.ruoyi.chengkao.service.impl;

import java.util.List;

import com.ruoyi.chengkao.domain.CkCandidateAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.chengkao.mapper.CkCandidateRegistrationMapper;
import com.ruoyi.chengkao.domain.CkCandidateRegistration;
import com.ruoyi.chengkao.service.ICkCandidateRegistrationService;

/**
 * 考生报名信息Service业务层处理
 * 
 * @author xiaosong
 * @date 2025-03-17
 */
@Service
public class CkCandidateRegistrationServiceImpl implements ICkCandidateRegistrationService 
{
    @Autowired
    private CkCandidateRegistrationMapper ckCandidateRegistrationMapper;
    @Autowired
    private CkCandidateAuditServiceImpl ckCandidateAuditServiceImpl;

    /**
     * 查询考生报名信息
     * 
     * @param id 考生报名信息主键
     * @return 考生报名信息
     */
    @Override
    public CkCandidateRegistration selectCkCandidateRegistrationById(Long id)
    {
        return ckCandidateRegistrationMapper.selectCkCandidateRegistrationById(id);
    }

    @Override
    public CkCandidateRegistration selectCkCandidateRegistrationByIdNumber(String idNumber)
    {
        return ckCandidateRegistrationMapper.selectCkCandidateRegistrationByIdNumber(idNumber);
    }

    /**
     * 查询考生报名信息列表
     * 
     * @param ckCandidateRegistration 考生报名信息
     * @return 考生报名信息
     */
    @Override
    public List<CkCandidateRegistration> selectCkCandidateRegistrationList(CkCandidateRegistration ckCandidateRegistration)
    {
        return ckCandidateRegistrationMapper.selectCkCandidateRegistrationList(ckCandidateRegistration);
    }

    /**
     * 新增考生报名信息
     * 
     * @param ckCandidateRegistration 考生报名信息
     * @return 结果
     */
    @Override
    public int insertCkCandidateRegistration(CkCandidateRegistration ckCandidateRegistration)
    {
        return ckCandidateRegistrationMapper.insertCkCandidateRegistration(ckCandidateRegistration);
    }

    /**
     * 修改考生报名信息
     * 
     * @param ckCandidateRegistration 考生报名信息
     * @return 结果
     */
    @Override
    public int updateCkCandidateRegistration(CkCandidateRegistration ckCandidateRegistration)
    {
        return ckCandidateRegistrationMapper.updateCkCandidateRegistration(ckCandidateRegistration);
    }

    //增加根据已有记录决定插入还是更新的upsert接口
    /**
     * 如果根据身份证号已经存在，则更新；否则新增
     */
    @Override
    public int upsertCkCandidateRegistration(CkCandidateRegistration registration , CkCandidateAudit audit)
    {
        // 根据考生身份证号判断记录是否存在
        CkCandidateRegistration existing = ckCandidateRegistrationMapper.selectCkCandidateRegistrationByIdNumber(registration.getIdNumber());
        if (existing != null)
        {
            // 存在则将主键设置到对象中，执行更新
            registration.setId(existing.getId());
            return ckCandidateRegistrationMapper.updateCkCandidateRegistration(registration);
        }
        else
        {
            // 不存在则新增记录
            // 检查审核记录是否已存在
            CkCandidateAudit existingAudit = ckCandidateAuditServiceImpl.selectCkCandidateAuditByCandidateNumber(audit.getCandidateNumber());
            if (existingAudit != null)
            {
                // 审核记录已存在，更新审核记录
                audit.setId(existingAudit.getId());
                ckCandidateAuditServiceImpl.updateCkCandidateAudit(audit);
            }
            else
            {
                // 审核记录不存在，插入新的审核记录
                ckCandidateAuditServiceImpl.insertCkCandidateAudit(audit);
            }
            // 插入报名记录
            return ckCandidateRegistrationMapper.insertCkCandidateRegistration(registration);
        }
    }

    /**
     * 批量删除考生报名信息
     * 
     * @param ids 需要删除的考生报名信息主键
     * @return 结果
     */
    @Override
    public int deleteCkCandidateRegistrationByIds(Long[] ids)
    {
        return ckCandidateRegistrationMapper.deleteCkCandidateRegistrationByIds(ids);
    }

    /**
     * 删除考生报名信息信息
     * 
     * @param id 考生报名信息主键
     * @return 结果
     */
    @Override
    public int deleteCkCandidateRegistrationById(Long id)
    {
        return ckCandidateRegistrationMapper.deleteCkCandidateRegistrationById(id);
    }
}
